package com.example.tourapp.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tourapp.Pojo.Mode_index;

import java.util.List;

public class Grid_Adapter extends BaseAdapter {
    private List<Mode_index> list;
    private Context context;
    private Integer img_id;
    private int layout_id;
    private Integer title_id;

    public Grid_Adapter(List<Mode_index> list, Context context, Integer id, int layout_id, Integer title_id) {
        this.list = list;
        this.context = context;
        this.img_id = id;
        this.layout_id = layout_id;
        this.title_id = title_id;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        viewHolder holder=null;

        if (view==null)
        {
            view= LayoutInflater.from(context).inflate(layout_id,viewGroup,false);
            holder=new viewHolder();
           holder.img=view.findViewById(img_id);
           //判断是否有标题id
            if (title_id!=null)
            {
                holder.title=view.findViewById(title_id);
            }
            view.setTag(holder);
        }else
        {
            holder = (viewHolder) view.getTag();
        }

        holder.img.setImageResource(list.get(i).getImg());
        if (title_id!=null)
        {
            holder.title.setText(list.get(i).getTitle());
        }
        return view;
    }
    class viewHolder{
        ImageView img;
        TextView title;
    }
}
